﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace MvcApplication.Controllers
{
    public class AuthorizeExtensionAttribute : AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.OnAuthorization(filterContext);
            CheckIfUserIsAuthenticated(filterContext);
        }

        private void CheckIfUserIsAuthenticated(AuthorizationContext filterContext)
        {
            if (filterContext.Result == null) return;

            if (filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                ViewResult viewResult = new ViewResult();
                viewResult.ViewName = "Error";
                filterContext.Result = viewResult;
            }
        }
    }
}
